﻿ 
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YiMeiJiaLibModel.Business;
using YiMeiJiaLibModel.Business.Dto;
using YiMeiJiaLibModel.Config;
using YiMeiJiaLibModel.Enums;
using YiMeiJiaService.Assembly;
using YiMeiJiaService.IService;


namespace YiMeiJiaApp.Pages.Account.My
{
    public partial class Address_Add
    {
        #region 服务组件

        [Inject] private IAcccount _account { get; set; }
        [Inject] private IGoods _goods { get; set; }

        [Inject] protected NavigationManager Navigation { get; set; }


        [Inject] protected IJSRuntime JSRuntime { get; set; }
        #endregion



        #region 绑定数据

        /// <summary>
        /// 需要新增的内容
        /// </summary>
        public SaveUserShipPost _ADD_address { get; set; }
        /// <summary>
        /// 需要编辑的内容
        /// </summary>
        public CoreCmsUserShipDto _EDIT_address { get; set;  }
       

        /// <summary>
        /// 是否编辑
        /// </summary>
        public  bool IsEditor { get; set;  }
        /// <summary>
        /// 是否有默认
        /// </summary>
        public bool hasDefault { get; set;  }
        /// <summary>
        /// 是否有地址
        /// </summary>
        public bool hasAddress { get; set; }


        /// <summary>
        /// 省市区
        /// </summary>
        public string CityAll { get; set;  }
        #endregion

        /// <summary>
        /// 加载事件
        /// </summary>
        /// <returns></returns>
        protected override async Task OnInitializedAsync()
        {
            _ADD_address = new SaveUserShipPost();
            try
            {
                IsEditor = false; // 默认为新增
                string msg = "all";
                var _address = await _HttpClient.Action<List<CoreCmsUserShipDto>>("获取用户收货地址列表", new { }, true);
                if (_address.status)
                {
                    hasAddress = _address.data.Any() ? true :false ;
                    var count = _address.data.Where(x => x.isDefault).ToList().Count();
                    if(count > 0) { hasDefault = true; } else
                    {
                        hasDefault = false;
                    }
                }
                var add = Common.GetParams(new Uri(Navigation.Uri).Query, "OpType");
                if (!string.IsNullOrEmpty(add) && add =="0")
                {
                    IsEditor = true;
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            await base.OnInitializedAsync();
        }
        /// <summary>
        /// 选择城市
        /// </summary>
        public  void   SetCity()
        {
              EvalJs.ExJs().EvalVoid(JSRuntime, @"
                                                $(""#city-picker"").cityPicker({title: ""请选择省市区""});
                                                $(""#city-picker"").click();
                                                ");
             
        }
        public async Task SaveAddress()
        {
            // SaveUserShipPost
            if(string.IsNullOrEmpty(_ADD_address.name))
            {
                await EvalJs.ExJs().Toast(JSRuntime, "请输入收件人的姓名", EmToast.cancel);
                return; 
            }
            if (string.IsNullOrEmpty(_ADD_address.mobile))
            {
                await EvalJs.ExJs().Toast(JSRuntime, "请输入收件人的手机号", EmToast.cancel);
                return;
            }
            var isMobile = _Verify.IsPhoneNumber(_ADD_address.mobile);
            if (!isMobile)
            {
                await EvalJs.ExJs().Toast(JSRuntime, "请输入手机号", EmToast.cancel);
                return;
            }
            if (string.IsNullOrEmpty(_ADD_address.address))
            {
                await EvalJs.ExJs().Toast(JSRuntime, "请输入详细地址（包含省市区）", EmToast.cancel);
                return;
            }
            //if (string.IsNullOrEmpty(CityAll))
            //{
            //    await EvalJs.ExJs().Toast(JSRuntime, "请选择省市区", EmToast.cancel);
            //    return;
            //}
            _ADD_address.areaId = 0;
            _ADD_address.longitude = "0,0";
            _ADD_address.latitude = "0,0";
            _ADD_address.street = CityAll;
            var result = await _HttpClient.Action<string>("保存收获地址", _ADD_address, true);
            if (result != null) {
                if (result.status)
                {
                    await EvalJs.ExJs().alert(JSRuntime, "添加成功");
                }
                else
                {
                    await EvalJs.ExJs().alert(JSRuntime, $"添加失败：{result.msg}");

                }
            }
        }
    }
}
